//
//  MaoCuhui2TableViewController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/9.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "MaoCuhui2TableViewController.h"
#import "MaoCuHuiModel.h"
#import "MaoCuHuiCell.h"
#import "FSMaoCHDetailViewController.h"
#import "chaxunmcViewController.h"

@interface MaoCuhui2TableViewController ()

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,assign) NSInteger currentPage;

@end

@implementation MaoCuhui2TableViewController

static NSString *const ID = @"Cell";

-(NSMutableArray *)dataArray{
    
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"贸促会";
    __weak typeof (self) weakSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
        
        
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        
        [weakSelf loadMore];
        
    }];
     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"]  style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
        [self.navigationItem setRightBarButtonItem:rightButton];

}
- (void)rightButtonAction{
chaxunmcViewController *vc  = [[chaxunmcViewController  alloc]init];
[self.navigationController pushViewController:vc animated:YES];

}
- (void)loadData{
    
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': mchList,'service': MemberService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,name:%@,endtime:%@,begintime:%@ }'}",userId,pwdId,userHumber,self.currentPage,_name,_timeafterstring,_timebeforestring];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        [self.dataArray removeAllObjects];
        NSArray *arr = JSON[@"reason"];
        
        for (NSDictionary *dic in arr) {
            
            NSDictionary *dict = dic[@"attrs"];
            
            MaoCuHuiModel *leftM = [MaoCuHuiModel mj_objectWithKeyValues:dict];
            
            [self.dataArray addObject:leftM];
            
        }
        
        [self.tableView headerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

- (void)loadMore{
    
    self.currentPage ++;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': mchList,'service': MemberService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,name:%@,endtime:%@,begintime:%@ }'}",userId,pwdId,userHumber,self.currentPage,_name,_timeafterstring,_timebeforestring];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"reason"];
        
        for (NSDictionary *dic in arr) {
            
            NSDictionary *dict = dic[@"attrs"];
            
            MaoCuHuiModel *leftM = [MaoCuHuiModel mj_objectWithKeyValues:dict];
            
            [self.dataArray addObject:leftM];
            
        }
        
        if (self.dataArray.count == 0) {
            
            [self.view makeToast:@"没有最新数据哦"];
        }
        
        
        [self.tableView footerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MaoCuHuiModel *model = self.dataArray[indexPath.row];
    
    MaoCuHuiCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [MaoCuHuiCell WithMaoCuHuiCell];
        
    }
    
    cell.model = model;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MaoCuHuiModel *model = self.dataArray[indexPath.row];

    FSMaoCHDetailViewController *maoVC = [[FSMaoCHDetailViewController alloc] init];

    maoVC.ID = model.ID;
    
    [self.navigationController pushViewController:maoVC animated:YES];

}





@end
















